<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@	page import="com.yaodian100.core.common.utils.ServerValue"%>
<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath()%>'/>
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<!-- 数字验证 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>
<style type="text/css" media="screen">
 <!--      
  select.selCont { margin: 10px; width: 780px; }
 -->
</style>
<script language="javascript">
	$().ready( function() {
		//商品编号栏位只能输入数值
		$("#pickProductId").numeric();
		$("#pickProductId").blur(function(){
			$(this).val(prodPadLeft($(this).val()));
		});
		//查询商品资料
		$("#findPickProduct").click(function(){
			var addData="";				
			$('#addOnSelect option').each(function(){
				addData = addData + $(this).val() + ";";										
			});
			$.post("${midContextPath}/pickProduct/findPickProductName.do",{pickProductId:$("#pickProductId").val(),productIdParam:"${product.productId}",isScm:"${product.isScm}",finalAddOnData:addData},pickProductCallback,"json");
		});
		//点选清除button
		$("#clearDataContext").click(function(){
			clear_form_elements();
		});
		//点选加入button
		$("#addSelect").click(function(){
			 if ($("#pickProductId").val() == "" ){
					alert("请输入搭选商品编号");
					return false;
			}
			if ( checkAddonId() ){
				return false;
			}
			var addOnDataText = $("#pickProductId").val()
							+","+$("#showPickProductName").text()
							+","+$("#pickProductDesc").val();

			var addOnDataValue = "";
			if ( $("#addonId").val() != ""){
				if ( !duplicatePickProductIdExist()){
					addOnDataValue=$("#pickProductId").val();
					$('#addOnSelect').append($("<option></option>").attr("value",addOnDataValue).text(addOnDataText));
				}else alert("请勿加入重复的n选m商品");
				clear_form_elements();
			}else
				alert("請確認商品狀態");
		});

		//編輯時select選取反白要修改的商品
		if($("#addonId").val()!=""){
			$('#addOnSelect option').each(function(){		
				if(this.value.split(",")[0]==$("#addonId").val()){	
					$(this).attr("selected","true"); 
				}									
			});
		}
		
		//移除select的商品	
		$("#removeSelect").click(function(){
			$("#addOnSelect option:selected").remove();	
			clear_form_elements();			
		});		
	
		//点选select里的item，自动将资料带到上方的栏位中
		$("#addOnSelect").click(function(){	
			$("#addonId").val($(this).val());			
			$("#pickProductId").val($('#addOnSelect option:selected').text().split(",")[0]);
			$("#showPickProductName").text($('#addOnSelect option:selected').text().split(",")[1]);
			var splitDesc=$('#addOnSelect option:selected').text().split(",")[2];
			$("#pickProductDesc").val(splitDesc.split("｜")[0]);
		});	
	
		//點選修改時動作
		$("#editedToSelect").click(function(){
			if ( $("#addOnSelect").find("option:selected").val() == undefined){
				alert("请选择要修改的搭售商品");
				return false;
			}
			if($("#showPickProductName").text() == ""){
				alert("请先确认搭选商品名称");
				return false;
			}
			if ( checkAddonId() ){
				return false;
			}
			var addData="";				
			$('#addOnSelect option').each(function(){
				addData = addData + this.value.split(",")[0] + ";";										
			});
			if(addData == "" || addData.indexOf($("#addonId").val()) == -1 ){
				alert("無現有商品,請按加入");
				return false;
			}

			var addOnDataValue = "";
			if ( $("#addonId").val() != ""){
				if ( $("#addOnSelect").find("option:selected").val() == $("#addonId").val()){
					addOnDataValue=$("#pickProductId").val();
					$('#addOnSelect').append($("<option></option>").attr("value",addOnDataValue).text(addOnDataText));
				}else{
					alert("请勿加入重复的n选m商品");
					clear_form_elements();
					return false;
				}
			}
			
			var addOnDataText = $("#pickProductId").val()
			+","+$("#showPickProductName").text()
			+","+$("#pickProductDesc").val()+"｜";
			var addOnDataValue = $("#addonId").val();
			$("#addOnSelect option:selected").val(addOnDataValue);
			$("#addOnSelect option:selected").text(addOnDataText);
			clear_form_elements();
		});	
		
		$("#submit_button").click(function(){
			var addData="";				
			$('#addOnSelect option').each(function(){
				addData = addData + $(this).text().split("｜")[0] + ";";										
			});
			if (addData == "" ){
				alert("请挑选n选m商品");
				return false;
			}
			$("#finalAddOnData").val(addData);
			//alert($("#finalAddOnData").val());
			//return false;
		});
	});
	//清空输入栏位
	function clear_form_elements(){
			ele = $("#pickProductForm");
	    $(ele).find(':input').each(function() {
	        switch(this.type) {
	            case 'password':
	            case 'select-multiple':
	            case 'select-one':
	            case 'text':
	            case 'textarea':
                $(this).val('');
                break;
	            case 'checkbox':
	            case 'radio':
	                this.checked = false;
	        }
	    }); 
	    $("#showPickProductName").text("");
	}
	//查询挑选的商品资讯
	function pickProductCallback(jsonProduct){
		if ( null == jsonProduct ){
			$("#showPickProductName").text("查无商品资料(或无该权限查看)");
			$("#addonId").val("");
		}else if ( !(jsonProduct.productType == 0 || jsonProduct.productType == 2 || jsonProduct.productType == 3 ) ){
			$("#showPickProductName").text("此商品無法被搭选:"+jsonProduct.productType);
			$("#addonId").val("");
		}else if ( jsonProduct.productType == 7 ){
			$("#showPickProductName").text("此商品無法被搭选:"+jsonProduct.productType);
			$("#addonId").val("");
		}else if ( jsonProduct.isDelete == "Y"){
			$("#showPickProductName").text("此商品已被删除");
			$("#addonId").val("");
		}else if ( jsonProduct.isDelete == "Q"){
			$("#showPickProductName").text("此商品的销售状态为可查询不可销售");
			$("#addonId").val("");
		}else if ( jsonProduct.productStatus != "PRD500"){
			$("#showPickProductName").text("商品状态不正确");
			$("#addonId").val("");
		}else if ( jsonProduct.productName == "请选择相同供应商或耀点100商品(000000,000953)，以及相同SCM状态的商品"){
			$("#showPickProductName").text(jsonProduct.productName);
			$("#addonId").val("");
		}else if ( jsonProduct.productName == "请选择入仓商品"){
			$("#showPickProductName").text(jsonProduct.productName);
			$("#addonId").val("");
		}else{
			$("#showPickProductName").text(jsonProduct.productName);
			$("#addonId").val(jsonProduct.productId);
		}
	}	

	function checkAddonId(){
		if ( $("#addonId").val() == "" ){
			alert($("#showPickProductName").text());
			return true;
		}else{
			return false;
		}
		/*
		if ( $("#showPickProductName").text() == "查无此商品" ||
				$("#showPickProductName").text().indexOf("此商品無法被搭选:") != -1 ||
				$("#showPickProductName").text() == "此商品已被删除" ||
				$("#showPickProductName").text() == "此商品的销售状态为可查询不可销售" ||
				$("#showPickProductName").text() == "商品状态不正确" || 
				$("#showPickProductName").text() == "请选择相同供应商或耀点100商品(000000,000953)，以及相同SCM状态的商品"
				){
			alert($("#showPickProductName").text());
			return true;
		}else{
			return false;
		}
		*/
	}
	//判断是否有重复的商品编号
	function duplicatePickProductIdExist(){
		var result = false;		
		$('#addOnSelect option').each(function(){		
			if( $(this).val() == $("#addonId").val()){	
				result = true;
			}									
		});	
		return result;
	}
	
</script>	
<h1>N选M商品管理-挑选N选M商品<br/>
	<div class="f-link">
		<a href="<%=ServerValue.getMidContextPath()%>/pickProduct/search.do">回列表页</a>
	</div>
</h1>
<s:actionerror />
<s:actionmessage />
<s:fielderror />
<br/>
<c-rt:if test="${not empty product.productId or not empty shelf.shelfId}">
	<jsp:include page="commonMenu.jsp"/>
</c-rt:if>
<s:form action="savePickProduct.do" id="pickProductForm" method="post">
	<table class="tb-data" width="100%">
		<tr>
			<th>* 搭选商品编号</th>
			<td>
				<input type="text" name="pickProductId"
				id="pickProductId" size="8" maxlength="8" value="${addOnProduct.mainProductId}" />
				<input type="button" id="findPickProduct" value="查询商品" />
			</td>
		</tr>
		<tr>
			<th>搭选商品名称</th>
			<td><span id="showPickProductName"></span></td>
		</tr>
		<tr>
			<th>简称(前台呈现使用)</th>
			<td><input type="text" id="pickProductDesc" name="pickProductDesc" size="40" maxlength="20" value="${addOnProduct.addonDesc}"/><span class="txt-rd">字数限制:20个中文字</span></td>
		</tr>
		<tr>
			<td colspan="2">
			<div class="f-link">
				<a href="javascript:void(0);" id="clearDataContext">清除</a>	
				<a href="javascript:void(0);" id="editedToSelect">修改</a>
				<a href="javascript:void(0);" id="addSelect">加入</a>
				<a href="javascript:void(0);" id="removeSelect">移除</a>
			</div>
		</tr>
		<tr>
			<th>* 搭选商品列表</th>
			<td>
				<!-- 新增時顯示的select -->
				<select id="addOnSelect" name="addOnSelect" size="50" class="selCont">
					<c-rt:forEach items="${addOnProducts}" var="addOnProduct">
						<option value="${addOnProduct.addOnProductId}">${addOnProduct.addOnProductId},${productNameMap[addOnProduct.addOnProductId]},${addOnProduct.addOnDesc}｜[${addOnProduct.createUser}/${addOnProduct.createDate}/${addOnProduct.modifyUser}/${addOnProduct.modifyDate}]</option>
					</c-rt:forEach>
				</select>
				<br/>[建立者/建立日期/修改者/修改日期]
			</td>
		</tr>		
		<tr>
			<td>
			<input type="hidden" id="addonId" name="addonId" value="" />
			<input type="hidden" name="productIdParam" value="${product.productId}" />
			<input type="hidden" name="shelfIdParam" value="${shelf.shelfId}" />
			<input type="hidden" name="finalAddOnData" id="finalAddOnData" value=""/>
			<input type="submit" id="submit_button" value="储  存" />								
			</td>
		</tr>
	</table>
</s:form>
